package com.huluobo.kotlinim1911a.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.huluobo.kotlinim1911a.R
import org.jetbrains.anko.sp

/**
 * Created by lc on 2022/6/28.
 */
class SlideBar(context: Context, attributeSet: AttributeSet? = null) : View(context, attributeSet) {
    companion object {
        private val SECTIONS = arrayOf(
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "J",
            "K",
            "L",
            "M",
            "N",
            "O",
            "P",
            "Q",
            "R",
            "S",
            "T",
            "U",
            "V",
            "W",
            "X",
            "Y",
            "Z"
        )
    }

    private var sectionHeight = 0f//给第一个item(section)高度一个初始值
    private var textBaseline = 0f//定义基准线

    private var paint = Paint()

    init {
        paint.apply {
            color = resources.getColor(R.color.qq_gray)
            textSize = sp(12).toFloat()

            //设置画笔的居中对齐
            textAlign = Paint.Align.CENTER
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        //计算每一个字符(section)分配的高度
        //h代表当前View的高度,字母条的高度
        sectionHeight = h * 1.0f / SECTIONS.size

        //绘制文字的尺寸
        val fontMetrics = paint.fontMetrics

        //计算绘制文本的高度
        val textHeight = fontMetrics.descent - fontMetrics.ascent

        //计算基准线
        textBaseline = sectionHeight / 2 + (textHeight / 2 - fontMetrics.descent)
    }

    override fun onDraw(canvas: Canvas?) {
        //绘制所有的字母
        val x = width * 1.0f / 2
//        var y = sectionHeight

        var baseline = textBaseline

        SECTIONS.forEach {
            canvas?.drawText(it, x, baseline, paint)
            //更新Y的高度
            baseline += sectionHeight
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                setBackgroundColor(R.drawable.bg_slide_bar)
                //找到点击的字母
                val index = getTouchIndex(event)
                val firstLetter = SECTIONS[index]
                onSectionChangeListener?.onSectionChange(firstLetter)
            }
            MotionEvent.ACTION_MOVE -> {
                //找到点击的字母
                val index = getTouchIndex(event)
                val firstLetter = SECTIONS[index]
                onSectionChangeListener?.onSectionChange(firstLetter)
            }
            MotionEvent.ACTION_UP -> {
                setBackgroundColor(Color.TRANSPARENT)
                onSectionChangeListener?.onSlideFinish()
            }
        }
        return true //触摸事件被消费掉
    }

    private fun getTouchIndex(event: MotionEvent): Int {
        var index = (event.y / sectionHeight).toInt()
        if (index < 0) {
            index = 0
        } else if (index >= SECTIONS.size) {
            index = SECTIONS.size - 1
        }
        return index
    }

    interface OnSectionChangeListener {
        fun onSectionChange(firstLetter: String)//回调出首字符
        fun onSlideFinish()//滑动结束的回调
    }

    //定义接口变量
    var onSectionChangeListener: OnSectionChangeListener? = null
}